import unicodedata

from django.shortcuts import render
from test1.models import Test
import test1.osu_data_get
import time

from django.views.decorators import csrf
from django.http import HttpResponse


def getIPFromDjangoRequest(request):
    if 'HTTP_X_FORWARDED_FOR' in request.META:
        return request.META['HTTP_X_FORWARDED_FOR']
    else:
        return request.META['REMOTE_ADDR']


def search_post(request):
    l1 = ['id', 'username', 'playmode', 'global_rank', 'country_rank', 'pp', 'ranked_score', 'hit_accuracy', 'play_count', 'play_time', 'total_score',
          'total_hits', 'maximum_combo']
    ctx = {}
    if request.POST and request.POST['q'] != '':
        t = time.gmtime()
        test01 = Test(search_name=request.POST['q'], search_time=time.strftime("%Y-%m-%d %H:%M:%S", t),
                      user_ip=getIPFromDjangoRequest(request))
        test01.save()
        ques_info = request.POST['q']
        d1 = test1.osu_data_get.get_osu_info(ques_info)
        d2 = {}
        if d1:
            for i in l1:
                d2[i] = d1[i]
        else:
            d2['no found!'] = 'no found!'
        ctx['info'] = d2
    return render(request, 'temp1.html', ctx)



